package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.presenter.BookPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.book.view.AdSearchVagueListLayout;

import java.util.List;

import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.base.baseui.SwipeBackActivity.ACTION_DESTORY;

/**
 * 搜索结果适配器
 * Created by wcy on 18/5/27.
 */
public class SearchResultAdapter extends BaseQuickAdapter<BookInfo> {
    private String mKeyWord;
    BookPresenter mPresenter;
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    public SearchResultAdapter(Context context, List<BookInfo> data) {
        super(context, data);
        mPresenter = new BookPresenter(lifeCyclerSubject);
    }

    @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        lifeCyclerSubject.onNext(ACTION_DESTORY);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_book_layout;
    }
    public void setKeyWord(String keyWord) {
        mKeyWord = keyWord;
    }
    @Override
    protected void convert(BaseViewHolder holder, BookInfo item, int position) {
        holder.setText(R.id.book_title, item.book_title).setText(R.id.book_desc, item.book_intro)
                .setText(R.id.author, item.author_name).setText(R.id.book_label, BookUtils.getBookLabel(item))
                .setText(R.id.book_status, BookUtils.getBookStatus(item));
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        BookUtils.setStatusBg(holder.getView(R.id.book_status), item);
        holder.setVisible(R.id.bottom_container, true);
        holder.setOnClickListener(R.id.join_shelf, v -> {
            mPresenter.joinBookShelf(item.book_id);
        });
        holder.getConvertView().setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.book_id);

        });
        AdSearchVagueListLayout.handleKeyWord(mKeyWord, holder, item);
        TextView join = holder.getView(R.id.join_shelf);
        if ("0".equals(item.join_bookcase)) {
            join.setEnabled(true);
            join.setText("加入书架");
        } else {
            join.setEnabled(false);
            join.setText("已加入书架");
        }
        DisplayUtils.visible(join);
    }
}
